export function getParams(type = false, url = window.location.href, isString) {
	if (url.indexOf('?') < 0) {
		return undefined;
	}
	const str = url.substr(url.indexOf('?') + 1);
	const params = new URLSearchParams(str);
	const obj = {};
	for (const item of params.entries()) {
		if (item[1] && item[1] !== 'null' && item[1] !== 'undefined') {
			obj[item[0]] =
				isNaN(Number(item[1])) || isString ? item[1] : Number(item[1]);
		}
	}
	if (type) {
		return obj[type];
	} else {
		return obj;
	}
}
export function turnToUrlparam(obj) {
	let urls = '';
	const hasValKey = Object.keys(obj)
		.filter(item => obj[item] || obj[item] === false || obj[item] === 0)
		.map(i => {
			return i + '=' + obj[i];
		});
	urls = hasValKey.join('&');
	return urls;
}
export function searchStringParams(params) {
	const str = Object.entries(params).reduce((cur, item) => {
		return (cur += `${item[0]}=${item[1] ? encodeURIComponent(item[1]) : ''}&`);
	}, '');
	const len = str.length;
	return str.slice(0, len - 1);
}
// 获取url参数
export const GetQueryString = (search, name) => {
	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
	var r = search.substr(1).match(reg); // search,查询？后面的参数，并匹配正则
	if (r != null) return decodeURI(r[2]);
	return null;
};
// url参数=>对象
export const GetQueryObject = search => {
	var keywords = search.substr(1);
	var result = keywords.replace(/&/g, '","').replace(/=/g, '":"');
	var reqDataString = '{"' + result + '"}';
	return JSON.parse(reqDataString);
};
